<script>
  var FoodRatings = function (foods, cuisines, ratings) {
    this.dirty = {};
    this.foods = {};
    this.cuisines = {};
    for (let i = 0; i < foods.length; i++) {
      this.dirty[cuisines[i]] = true;
      const f = {
        food: foods[i],
        cuisine: cuisines[i],
        rating: ratings[i],
      };
      this.foods[foods[i]] = f;
      const name = foods[i];
      this.cuisines[cuisines[i]] ? this.cuisines[cuisines[i]].push(f) : this.cuisines[cuisines[i]] = [f];
    }
  };

  /** 
   * @param {string} food 
   * @param {number} newRating
   * @return {void}
   */
  FoodRatings.prototype.changeRating = function (food, newRating) {
    this.foods[food].rating = newRating;
    this.dirty[this.foods[food].cuisine] = true;
  };

  /** 
   * @param {string} cuisine
   * @return {string}
   */
  FoodRatings.prototype.highestRated = function (cuisine) {
    if (this.dirty[cuisine]) {
      this.cuisines[cuisine].sort((a, b) => {
        if (b.rating != a.rating) {
          return b.rating - a.rating;
        } else {
          return b.food < a.food ? 1 : -1;
        }
      })
    }
    return this.cuisines[cuisine][0].food;
  };
  const foodRatings = new FoodRatings(["kimchi", "miso", "sushi", "moussaka", "ramen", "bulgogi"], ["korean", "japanese", "japanese", "greek", "japanese", "korean"], [9, 12, 8, 15, 14, 7]);
  foodRatings.highestRated("korean"); // 返回 "kimchi" 是分数最高的韩式料理，评分为 9 。
  foodRatings.highestRated("japanese"); // 返回 "ramen" 是分数最高的日式料理，评分为 14 。
  foodRatings.changeRating("sushi", 16); // "sushi" 现在评分变更为 16 。
  foodRatings.highestRated("japanese"); // 返回 "sushi" 是分数最高的日式料理，评分为 16 。
  foodRatings.changeRating("ramen", 16); // "ramen" 现在评分变更为 16 。
  foodRatings.highestRated("japanese");
</script>